package cn.bnjy.core;

import cn.bnjy.common.EasyData;
import cn.bnjy.entity.Config;
import cn.bnjy.service.IConfigService;
import cn.bnjy.util.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;

/**
 * 描述：初始化执行器
 *
 * @date: 2018/11/6 23:47
 * @author: chavin
 */
@Slf4j
@Component
public class InitRunner implements CommandLineRunner {

    @Autowired
    private IConfigService configService;

    /**
     * 初始化数据
     * @param args
     * @throws Exception
     */
    @Override
    public void run(String... args) {
        log.info("初始化开始");
        LocalDateTime startTime = LocalDateTime.now();
        try {
            List<Config> configList = configService.list(null);
            // 初始化配置表到EasyData.CONFIG_MAP中
            if(CollectionUtils.isNotBlankList(configList)) {
                configList.forEach(config -> EasyData.CONFIG_MAP.put(config.getCKey(), config.getCValue()));
                log.info("初始化配置表数据成功");
            }
        } catch (Exception e) {
            log.error("初始化异常，异常：{}", e);
        }
        LocalDateTime endTime = LocalDateTime.now();
        log.info("初始化结束，耗时：{}ms", ChronoUnit.MILLIS.between(startTime, endTime));
    }

}

